<?php
/**
 * Created by PhpStorm.
 * User: jiangxiaobo
 * Date: 2019/11/18 0018
 * Time: 17:49
 */
namespace routes;

abstract class RoutePredefined
{
    // 配置路由和控制器的映射关系
    abstract protected function configMap();
    // 配置路由和目标匹配值
    abstract protected function configSubject();
    // 检测该路由是否可用
    abstract protected function check($pattern);
    // 获取当前路由所映射的控制器及方法
    abstract protected function getController($route);
    // 路由触发
    public function trigger()
    {
        // 1. 遍历所有配置的路由
        foreach($this->configMap() as $pattern => $route)
        {
            // 2. 检测该路由是否可用
            if($this->check($pattern))
            {
                // 3. 根据路由匹配项实例化控制器
                $t = $this->getController($route);
                // 反射实例化控制器类
                $reflectionClass = new \ReflectionClass($t[0]);
                $cInstance = $reflectionClass->newInstance();
                if(!method_exists($cInstance,$t[1])){
                    throw new \Exception("{$t[0]}控制器{$t[1]}方法不存在");
                }
//                    call_user_func_array([$cInstance,$a],[param1,param2]);
//                    call_user_func([$cInstance,$a],param1,param2);
                call_user_func([$cInstance,$t[1]]);
                return true;
            }
        }

        header("HTTP/1.1 404 Not Found");
        header("Status: 404 Not Found");
        return false;
    }
}
